using System;
using System.Collections;
using System.IO;

namespace Pegasus.Diagnostics.Telnet
{
	/// <summary>
	/// Implements the Quit command for the telnet service
	/// </summary>
	internal class CommandHelp : ITelnetCommand
	{
		// Local Instanc Values
		private TelnetProcessor m_processor;

		/// <summary>
		/// Default Constructor
		/// </summary>
		/// <param name="processor">The telnet process handing this session</param>
		public CommandHelp( TelnetProcessor processor )
		{
			m_processor = processor;
		}

		/// <summary>
		/// The names of the command that the user types in.
		/// </summary>
		public string Command
		{
			get
			{
				return "help";
			}
		}

		/// <summary>
		/// Any short cut commands or key (i.e. quit uses q and exit)
		/// </summary>
		public string[] CommandShortCuts
		{
			get
			{
				return new string[] { "?" };
			}
		}

		/// <summary>
		/// Execute the command.
		/// </summary>
		/// <param name="args">Additional parameters that the user typed in.</param>
		/// <param name="writer">Text stream to write to</param>
		public void ExecCommand( TextWriter writer, string[] args )
		{
			if( args.Length == 0 )
			{
				ShowCommands( writer );
			}
			else
			{
				ShowDetails( writer, args[ 0 ] );
			}
		}

		/// <summary>
		/// Show a single line of help about the command
		/// </summary>
		/// <returns>Single line brief description of command</returns>
		public string GetHelp()
		{
			return "Show the available commands or details about a specific command.";
		}

		/// <summary>
		/// Show a the full usage help about the command
		/// </summary>
		/// <param name="writer">Text stream to write to</param>
		public void ShowDetailedHelp( TextWriter writer )
		{
			writer.WriteLine( "Command: help | ?" );
			writer.WriteLine( "Usage: help [command]" );
			writer.WriteLine();
			writer.WriteLine( "Description:  Help shows all the available commands that can" );
			writer.WriteLine( "be used.  Or the detailed help if the command is listed." );
		}

		/// <summary>
		/// Show a list of registered commands.
		/// </summary>
		/// <param name="writer">Text stream to write to</param>
		private void ShowCommands( TextWriter writer )
		{
			writer.WriteLine();
			writer.WriteLine( "Command    - Description" );
			writer.WriteLine();

			foreach( DictionaryEntry entry in m_processor.Commands )
			{
				string name = (string) entry.Key;
				ITelnetCommand command = (ITelnetCommand ) entry.Value;

				writer.WriteLine( "{0,-10} - {1}", name, command.GetHelp() );
			}

			writer.WriteLine();
		}

		/// <summary>
		/// Show the details for the help commands.
		/// </summary>
		/// <param name="writer">Text stream to write to</param>
		/// <param name="text">Name of the command to show help for.</param>
		private void ShowDetails( TextWriter writer, string text )
		{
			ITelnetCommand command = (ITelnetCommand) m_processor.Commands[ text ];
			if( command != null )
			{
				writer.WriteLine();
				command.ShowDetailedHelp( writer );
				writer.WriteLine();
			}
			else
			{
				writer.WriteLine();
				writer.WriteLine( "Unknown command " + text );
				writer.WriteLine();
			}
		}
	}
}
